<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学校ID，关联eas_school表的id字段" prop="schoolId">
      <el-input v-model="dataForm.schoolId" placeholder="学校ID，关联eas_school表的id字段"></el-input>
    </el-form-item>
    <el-form-item label="年级ID，关联eas_grade表的id字段" prop="gradeId">
      <el-input v-model="dataForm.gradeId" placeholder="年级ID，关联eas_grade表的id字段"></el-input>
    </el-form-item>
    <el-form-item label="班级ID，关联eas_class表的id字段" prop="classId">
      <el-input v-model="dataForm.classId" placeholder="班级ID，关联eas_class表的id字段"></el-input>
    </el-form-item>
    <el-form-item label="学年，例如：2023学年" prop="academicYear">
      <el-input v-model="dataForm.academicYear" placeholder="学年，例如：2023学年"></el-input>
    </el-form-item>
    <el-form-item label="学期，例如：1表示第一学期，2表示第二学期" prop="semester">
      <el-input v-model="dataForm.semester" placeholder="学期，例如：1表示第一学期，2表示第二学期"></el-input>
    </el-form-item>
    <el-form-item label="评语内容" prop="comment">
      <el-input v-model="dataForm.comment" placeholder="评语内容"></el-input>
    </el-form-item>
    <el-form-item label="评语字数" prop="commentNum">
      <el-input v-model="dataForm.commentNum" placeholder="评语字数"></el-input>
    </el-form-item>
    <el-form-item label="关键词个数" prop="keywordsNum">
      <el-input v-model="dataForm.keywordsNum" placeholder="关键词个数"></el-input>
    </el-form-item>
    <el-form-item label="所属老师ID，关联eas_basic_teacher表的id字段" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="所属老师ID，关联eas_basic_teacher表的id字段"></el-input>
    </el-form-item>
    <el-form-item label="学生ID，关联eas_basic_student表的id字段" prop="studentId">
      <el-input v-model="dataForm.studentId" placeholder="学生ID，关联eas_basic_student表的id字段"></el-input>
    </el-form-item>
    <el-form-item label="关键词" prop="keywords">
      <el-input v-model="dataForm.keywords" placeholder="关键词"></el-input>
    </el-form-item>
    <el-form-item label="是否展示给学生" prop="showStu">
      <el-input v-model="dataForm.showStu" placeholder="是否展示给学生"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          schoolId: '',
          gradeId: '',
          classId: '',
          academicYear: '',
          semester: '',
          comment: '',
          commentNum: '',
          keywordsNum: '',
          teacherId: '',
          studentId: '',
          keywords: '',
          showStu: '',
          createTime: '',
          createBy: '',
          updateTime: '',
          updateBy: ''
        },
        dataRule: {
          schoolId: [
            { required: true, message: '学校ID，关联eas_school表的id字段不能为空', trigger: 'blur' }
          ],
          gradeId: [
            { required: true, message: '年级ID，关联eas_grade表的id字段不能为空', trigger: 'blur' }
          ],
          classId: [
            { required: true, message: '班级ID，关联eas_class表的id字段不能为空', trigger: 'blur' }
          ],
          academicYear: [
            { required: true, message: '学年，例如：2023学年不能为空', trigger: 'blur' }
          ],
          semester: [
            { required: true, message: '学期，例如：1表示第一学期，2表示第二学期不能为空', trigger: 'blur' }
          ],
          comment: [
            { required: true, message: '评语内容不能为空', trigger: 'blur' }
          ],
          commentNum: [
            { required: true, message: '评语字数不能为空', trigger: 'blur' }
          ],
          keywordsNum: [
            { required: true, message: '关键词个数不能为空', trigger: 'blur' }
          ],
          teacherId: [
            { required: true, message: '所属老师ID，关联eas_basic_teacher表的id字段不能为空', trigger: 'blur' }
          ],
          studentId: [
            { required: true, message: '学生ID，关联eas_basic_student表的id字段不能为空', trigger: 'blur' }
          ],
          keywords: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          showStu: [
            { required: true, message: '是否展示给学生不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easstucomments/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.schoolId = data.easStuComments.schoolId
                this.dataForm.gradeId = data.easStuComments.gradeId
                this.dataForm.classId = data.easStuComments.classId
                this.dataForm.academicYear = data.easStuComments.academicYear
                this.dataForm.semester = data.easStuComments.semester
                this.dataForm.comment = data.easStuComments.comment
                this.dataForm.commentNum = data.easStuComments.commentNum
                this.dataForm.keywordsNum = data.easStuComments.keywordsNum
                this.dataForm.teacherId = data.easStuComments.teacherId
                this.dataForm.studentId = data.easStuComments.studentId
                this.dataForm.keywords = data.easStuComments.keywords
                this.dataForm.showStu = data.easStuComments.showStu
                this.dataForm.createTime = data.easStuComments.createTime
                this.dataForm.createBy = data.easStuComments.createBy
                this.dataForm.updateTime = data.easStuComments.updateTime
                this.dataForm.updateBy = data.easStuComments.updateBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easstucomments/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'schoolId': this.dataForm.schoolId,
                'gradeId': this.dataForm.gradeId,
                'classId': this.dataForm.classId,
                'academicYear': this.dataForm.academicYear,
                'semester': this.dataForm.semester,
                'comment': this.dataForm.comment,
                'commentNum': this.dataForm.commentNum,
                'keywordsNum': this.dataForm.keywordsNum,
                'teacherId': this.dataForm.teacherId,
                'studentId': this.dataForm.studentId,
                'keywords': this.dataForm.keywords,
                'showStu': this.dataForm.showStu,
                'createTime': this.dataForm.createTime,
                'createBy': this.dataForm.createBy,
                'updateTime': this.dataForm.updateTime,
                'updateBy': this.dataForm.updateBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
